package com.neusoft.databus.core.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.databus.common.dto.DictionaryDTO;
import com.neusoft.databus.common.enums.DictionaryType;
import com.neusoft.databus.core.service.DictionaryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 字典管理控制器
 *
 * @author sunchf
 * @date 2019年3月18日 下午1:16:41
 */

@Api("字典管理控制器")
@RestController
@RequestMapping(value = "/api/w/dictionaries")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 字典列表 <br>
     * GET <br>
     *
     * @return ResultListDTO
     */
    @ApiOperation("字典列表")
    @GetMapping("/{dictionaryType}")
    public ResultListDTO<DictionaryDTO> index(@PathVariable final String dictionaryType, final Searchable searchable) {
        List<DictionaryDTO> dtos = Lists.newArrayList();
        if (dictionaryType.equals(DictionaryType.unit.toString())) {
            dtos = this.dictionaryService.findByDictionaryTypeAndParentIsNull(DictionaryType.valueOf(dictionaryType));
        } else {
            dtos = this.dictionaryService
                    .findByDictionaryTypeAndParentIsNotNull(DictionaryType.valueOf(dictionaryType));
        }
        final ResultListDTO<DictionaryDTO> result = ResultListDTO.success(dtos);
        return result;
    }

}
